package com.woniuxy.student.provider.controller;

import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.Student;
import com.woniuxy.student.provider.service.StudentService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 作者
 * @create 2025-06-26 12:12
 */
@Slf4j
@RequestMapping("/student")
@RestController
public class StudentController {


    @Autowired
    StudentService studentService;

    @Value("${server.port}")
    int port;

    @GetMapping("/")
    public ResponseResult<List<Student>> getAll() {
        log.info("{}----查询所有学生信息", port);
        return ResponseResult.ok(studentService.getAll());
    }

    @GetMapping("/{sid}")
    public ResponseResult<Student> getBySid(@PathVariable("sid") int sid) {
        log.info("{}----查询所有学生信息", port);
        if (sid == 2) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        if (sid == 3) {
            throw new RuntimeException("业务处理异常");
        }
        return ResponseResult.ok(studentService.getBySid(sid));

    }

    @PostMapping("/")
    public ResponseResult<Void> add(@RequestBody Student student) {
        studentService.add(student);
        return ResponseResult.ok();
    }
    @GetMapping("/test")
    public ResponseResult<Void> test(HttpServletRequest request, HttpSession session) {
        log.info("{}--sessionid={}--sessionid={}", port,request.getSession().getId(),session.getId());
        return ResponseResult.ok();
    }
}
